//
//  SEGGuidePageVC.m
//  UhomebkFrame
//
//  Created by Zhuge_Magic on 2019/11/19.
//

#import "SEGGuidePageVC.h"

@interface SEGGuidePageVC ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) UICollectionView * collectionView;
@property (nonatomic,strong) NSArray<NSString*> * images;
@property (nonatomic, strong)UIPageControl *pageControl;
@end

static NSString * const kCellID = @"SEGGuideImageCell";

@implementation SEGGuidePageVC{
    NSInteger _currentIndex;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.images = @[@"seg_guide01",@"seg_guide01",@"seg_guide01"];
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    self.collectionView.scrollEnabled = self.images.count>1;
    [self.view addSubview:self.pageControl];
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nextPage)];
//    [self.collectionView addGestureRecognizer:tap];
}
- (void)nextPage{
    if (_currentIndex==self.images.count-1) return;
    _currentIndex ++;
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:_currentIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}
// 减速完成
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat offsetX = scrollView.contentOffset.x;
    // 获取角标
    _currentIndex = offsetX / kScreenWidth;
    _pageControl.currentPage = _currentIndex;
}

#pragma mark UICollectionViewDelegate/DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.images.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SEGGuideImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
    if (self.images.count>0) {
        cell.seg_image = self.images[indexPath.row];
        cell.showBtn = indexPath.row==self.images.count-1;
        kWeakSelf(self)
        cell.GuideImageCellBlock = ^{
            [weakself startApp];
            
        };
    }
    return cell;
}
- (void)startApp{
    if (self.GuideBlock)self.GuideBlock();
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isShowGuide"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(kScreenWidth, kScreenHeight);
}

#pragma mark LazyMethods
- (UICollectionView *)collectionView{
    if (_collectionView == nil) {
        // 创建布局
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        layout.itemSize = CGSizeMake(kScreenWidth, kScreenHeight);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        UICollectionView *contentScrollView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView = contentScrollView;
        // 设置内容滚动视图
        _collectionView.pagingEnabled = YES;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.bounces = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollsToTop = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.scrollEnabled = YES;//只允许Tabbar切换
        // 注册cell
        [_collectionView registerClass:[SEGGuideImageCell class] forCellWithReuseIdentifier:kCellID];
    }
    return _collectionView;
}

- (UIPageControl *)pageControl{
    if(!_pageControl){
        _pageControl= [[UIPageControl alloc]init];
        _pageControl.frame= CGRectMake(0, self.view.height-30-18, self.view.width, 18);
        _pageControl.numberOfPages = self.images.count;
        _pageControl.currentPage = 0;
        _pageControl.pageIndicatorTintColor = KColor_4;
        _pageControl.currentPageIndicatorTintColor = KColor_B;
    }
    return _pageControl;
}


@end
@interface SEGGuideImageCell ()

@property (nonatomic,weak) UIImageView * segImageView;
@property (nonatomic,strong) UIButton * startBtn;/**<<#Remark#>*/

@end

@implementation SEGGuideImageCell

- (void)setSeg_image:(NSString *)seg_image{
    _seg_image = seg_image;
    self.segImageView.image = [UIImage imageNamed:seg_image];
}
- (void)setShowBtn:(BOOL)showBtn{
    _showBtn = showBtn;
    self.startBtn.hidden = !showBtn;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self initViews];
    }return self;
}
- (void)initViews{
    UIImageView * imageView = [[UIImageView alloc] init];
    imageView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:imageView];
    self.segImageView = imageView;
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    [self addStartBtn];
}
- (void)addStartBtn{
    UIButton * btn = [[UIButton alloc] init];
    [self.contentView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(200, 44));
        make.bottom.offset(-(0.09*kScreenHeight));
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
    }];
    self.startBtn = btn;
    [btn setTitle:@"立即开启" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    btn.backgroundColor = KColor_B;
    btn.layer.cornerRadius = 3;
    btn.layer.masksToBounds = YES;
    [btn addTarget:self action:@selector(clickedStartBtn) forControlEvents:UIControlEventTouchUpInside];
    btn.hidden = YES;
    [self.contentView bringSubviewToFront:btn];
}
- (void)clickedStartBtn{
    if (self.GuideImageCellBlock)self.GuideImageCellBlock();
}


@end
